package quizman.tdg;
/**
 * 
 */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.dsrg.ts.threadLocal.DbRegistry;

/**
 * @author Team 0i
 *
 */
public class NumericAnswerTDG {
	public static ResultSet find(String questionName) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to NumericAnswerTDG.find.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("SELECT * from Possible_Answer where Question like ?");
			dbStatement.setString(1, questionName);
			ResultSet rs = dbStatement.executeQuery();
			return rs;
		} catch (Exception e) {
			String msg = "Error in NumericAnswerTDG.find";
			throw new Exception(msg, e);
		}
	}
	
	public static void insert(String questionName, double lowerBound, double upperBound) throws Exception {
		try {
			if(questionName==null || questionName.equals("") || lowerBound > upperBound)
				throw new IllegalArgumentException("Invalid argument to NumericAnswerTDG.insert.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("INSERT INTO Possible_Answer (isRight, Answer_Text, Question) values(?, ?, ?)");
			dbStatement.setString(1, "TRUE");
			dbStatement.setString(2, Double.toString(lowerBound));
			dbStatement.setString(3, questionName);
			dbStatement.executeQuery();
			
			dbStatement.setString(1, "FALSE");
			dbStatement.setString(2, Double.toString(upperBound));
			dbStatement.setString(3, questionName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in NumericAnswerTDG.insert";
			throw new Exception(msg, e);
		}
	}
	
	public static void delete(String questionName) throws Exception {
		try {
			if(questionName==null || questionName.equals(""))
				throw new IllegalArgumentException("Invalid argument to NumericAnswerTDG.delete.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("DELETE FROM Possible_Answer WHERE Question like ?");
			dbStatement.setString(1, questionName);
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in NumericAnswerTDG.delete";
			throw new Exception(msg, e);
		}
	}
	
	public static void update(String questionName, double lowerBound, double upperBound) throws Exception {
		try {
			if(questionName==null || questionName.equals("") || lowerBound > upperBound)
				throw new IllegalArgumentException("Invalid argument to NumericAnswerTDG.update.");
			Connection db = DbRegistry.getDbConnection();
			PreparedStatement dbStatement =
				db.prepareStatement("UPDATE TABLE Possible_Answer SET Answer_Text = ? , isRight = ? WHERE Question like ? AND isRight = ?");
			dbStatement.setString(1, Double.toString(lowerBound));
			dbStatement.setString(2, "TRUE");
			dbStatement.setString(3, questionName);
			dbStatement.setString(4, "TRUE");
			dbStatement.executeQuery();
			dbStatement.setString(1, Double.toString(upperBound));
			dbStatement.setString(2,"FALSE");
			dbStatement.setString(4,"FALSE");
			dbStatement.executeUpdate();
		} catch (Exception e) {
			String msg = "Error in NumericAnswerTDG.update";
			throw new Exception(msg, e);
		}
	}
}